//
//  checkInput.m
//  serialTest
//
//  Created by mt-007 on 2021/3/22.
//

#import "checkInput.h"

@implementation checkInput


/**
 * @param inputBaudRate a baud rate of serial port. limit {2400, 4800, 9600, 19200, 38400, 57600, 115200}.
 * @param inputDataBit a data bit of serial port. limit {7, 8}.
 * @param inputCheckParity a check parity of serial port. limit {'N', 'O', 'E'}.
 * @param inputStopBit a stop bit of serial port. limit {1, 2}.
 *
 * @return TRUE is everything is right or FALSE is mean something wrong.
 *
 * @brief check the config is right, is not will return FALSE and console will print the information in detail.
 */
- (BOOL)checkConfigInputRight:(int)inputBaudRate
                   andDataBit:(int)inputDataBit
               andCheckParity:(char)inputCheckParity
                   andStopBit:(int)inputStopBit{
    if(
       [self checkBaudRate: inputBaudRate] &&
       [self checkDataBit: inputDataBit] &&
       [self checkCheckParity: inputCheckParity] &&
       [self checkStopBit: inputStopBit]
       ) {
        return TRUE;
    } else {
        return FALSE;
    }
    
}

/**
 * @brief self function, check the baud rate is not right.
 */
- (BOOL)checkBaudRate:(int)baudRate {
    NSArray *rightArray = @[@2400, @4800, @9600, @19200, @38400, @57600, @115200];
    NSNumber *inpBaudRate = [NSNumber numberWithInt: baudRate];
    BOOL isHave = [rightArray containsObject: inpBaudRate];
    if(!isHave) {
        NSLog(@"input error: only have 2400, 4800, 9600, 19200, 38400, 57600, 115200 but your input is %d", baudRate);
    }
    return isHave;
}

/**
 * @brief self function, check the date bit is not right.
 */
- (BOOL)checkDataBit:(int)dataBit {
    NSArray *rightArray = @[@7, @8];
    NSNumber *inpDataBit = [NSNumber numberWithInt: dataBit];
    BOOL isHave = [rightArray containsObject: inpDataBit];
    if(!isHave) {
        NSLog(@"input error: only have 7, 8 but your input is %d", dataBit);
    }
    return isHave;
}

/**
 * @brief self function, check the check parity is not right.
 */
- (BOOL)checkCheckParity:(char)checkParity {
    NSArray *rightArray = @[@"N", @"O", @"E"];
    NSString *inpCheckParity = [NSString stringWithFormat:@"%c",checkParity];
    BOOL isHave = [rightArray containsObject: inpCheckParity];
    if(!isHave) {
        NSLog(@"input error: only have N, O, E but your input is %c", checkParity);
    }
    return isHave;
}

/**
 * @brief self function, check the stop bit is not right.
 */
- (BOOL)checkStopBit:(int)stopBit {
    NSArray *rightArray = @[@1, @2];
    NSNumber *inpStopBit = [NSNumber numberWithInt: stopBit];
    BOOL isHave = [rightArray containsObject: inpStopBit];
    if(!isHave) {
        NSLog(@"input error: only have 1, 2 but your input is %d", stopBit);
    }
    return isHave;
}

@end
